package com.example.quartz.schedulestarter;

import com.example.quartz.entity.JobInfo;
import com.example.quartz.entity.TriggerInfo;
import com.example.quartz.jobbuilder.MyJobBuilder;
import com.example.quartz.test.TestEntity;
import lombok.extern.slf4j.Slf4j;
import org.quartz.Scheduler;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.ApplicationArguments;
import org.springframework.boot.ApplicationRunner;
import org.springframework.stereotype.Component;

@Component
@Slf4j
public class ScheduleStarter implements ApplicationRunner {
    @Autowired
    private Scheduler scheduler;


    @Autowired
    private MyJobBuilder myJobBuilder;

    @Override
    public void run(ApplicationArguments args) throws Exception {
        // 1
        JobInfo jobInfo = new JobInfo();
        jobInfo.setJobName("jb-1");
        jobInfo.setJobGroup("jb-group-1");

        TriggerInfo triggerInfo = new TriggerInfo();
        triggerInfo.setTriggerName("t-1");
        triggerInfo.setTriggerGroup("t-group-1");

        TestEntity testEntity = new TestEntity();
        testEntity.setName("李四-1");
        myJobBuilder.register(jobInfo,  triggerInfo, testEntity);

        // 2
        JobInfo jobInfo1 = new JobInfo();
        jobInfo1.setJobName("jb-2");
        jobInfo1.setJobGroup("jb-group-1");

        TriggerInfo triggerInfo1 = new TriggerInfo();
        triggerInfo1.setTriggerName("t-2");
        triggerInfo1.setTriggerGroup("t-group-1");

        TestEntity testEntity1 = new TestEntity();
        testEntity1.setName("李四-2");
        myJobBuilder.register(jobInfo1,  triggerInfo1, testEntity1);

        scheduler.start();
        System.out.println("schedule 被启动了，" + scheduler);
    }
}
